fix: sanitize terminal escape sequences in HTTP response output#1897
Open
muhamedfazalps wants to merge 1 commit into
Open
fix: sanitize terminal escape sequences in HTTP response output#1897muhamedfazalps wants to merge 1 commit into
muhamedfazalps wants to merge 1 commit into
Conversation
A malicious HTTP server could embed ANSI escape codes or OSC sequences in response headers or body text, potentially: - Manipulating the terminal display (clearing screen, fake content) - Changing the terminal title - Injecting clipboard content on supported terminals - Exploiting terminal emulator vulnerabilities This fix strips terminal control sequences when outputting to a TTY, preventing escape sequence injection attacks. The sanitization is only applied when stdout is a terminal (not when piped or redirected), preserving raw output for scripting use cases. Fixes httpie#1812
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1812
A malicious HTTP server could embed ANSI escape codes or OSC sequences in response headers or body text, potentially:
\x1b]2;text\x07)This is particularly relevant for HTTPie since it's commonly used in security research and API testing with untrusted servers.
Fix
This PR adds a
_sanitize_for_terminal()function that strips terminal control sequences when outputting to a TTY:\t,\n,\r):\x00-\x08,\x0b,\x0c,\x0e-\x1f,\x7f\x1b[...(color codes, cursor movement)\x1b]...\x07(title, clipboard)Sanitization is applied to:
BaseStream.get_headers())EncodedStream.iter_body())BaseStream.get_metadata())Only applied when stdout is a TTY — piped/redirected output preserves raw data for scripting.
---\n\nbuymeacoffee.com/muhamedfazalps